from yacf.Tools.Dump import Dump

import config

#from yacf.Frontend.Parse import parse_source
from yacf.Frontend.FrontendFactory import FrontendFactory
from yacf.Frontend.Common.IRNode import *
from yacf.Frontend.SymbolTable import *
from yacf.Frontend.InternalRepr import AstToIR


TEST_PATH = config.FRONTEND_DIR + '/testsC99'
#CODE_PATH =  TEST_PATH + 'codes/'
#TREE_PATH =  TEST_PATH + 'trees/'

import unittest

from yacf.Backends.Common.tests.common import TestCase
from yacf.Backends.C.Writers.CWriter import CWriter

config.CURRENT_LANGUAGES = ['C99','Omp']

class TestSymbolTable(TestCase):

     def setUp(self):
          pass

     def test_symbolTable(self):
          """ Test symbol table """

          template_code = open(TEST_PATH + '/4_declarations.c', 'r').read()
          [ast, prepro_class, lexer_class, parser_class] = FrontendFactory().\
                        parse_with_language_list(template_code, 'declarations',
                        config.CURRENT_LANGUAGES)
          migrator = AstToIR(Writer = CWriter, ast = ast)
          migrator.annotate()
          st = migrator.symbolTable
          new_ast = migrator.ast

          current = str(st.lookUp(new_ast.ext[-1].body.block_items[4].lvalue))
          self.check_output(current, '{a: double, (20, 74)}')
          current = str(st.lookUp(new_ast.ext[-1].body.block_items[5].init.decls[0]))
          self.check_output(current, '{h: int, (42, 74)}')

     def test_all(self):
          """ Test all test codes """
          for test_code in [#'1_comments.c','2_longtypes.c',
                            '3_literals.c', '4_declarations.c',
                            #'5_omp.c','6_omp_default.c', 
                            '7_typedef.c', 
                            #'8_llc_nc_result.c', '9_clock.c'
                           ]:
                template_code = open(TEST_PATH + '/' + test_code, 'r').read()
                [ast, prepro_class, lexer_class, parser_class] = FrontendFactory().\
                        parse_with_language_list(template_code, test_code,
                        config.CURRENT_LANGUAGES)
                migrator = AstToIR(Writer = CWriter, ast = ast)
                migrator.annotate()


if __name__ == '__main__':
    test_suite = unittest.TestLoader().loadTestsFromTestCase(TestSymbolTable)
    unittest.TextTestRunner(verbosity=2).run(test_suite)
